/*
 * @Author       : ljkt
 * @Version      : 1.0
 * @Date         : 2021-10-05 18:00:43
 * @LastEditors: ljkt
 * @LastEditTime: 2024-04-28 10:53:05
 * @Description  :
 */

#include "Dev_Include.h"

s_dev_lh5815_t gDev_Lh5815;

void Dev_Lh5815_Init(void)
{
    Dev_Lh5815_Uart_Init();
    my_memclr_00(&gDev_Lh5815, sizeof(gDev_Lh5815));
}
void Dev_Lh5815_Rx_Deal(void)
{
    Ram u8_t len = 0;
    Ram u16_t crc = 0;
    s_drv_lt5815_read_reg1_50_page_t *read = ((s_drv_lt5815_read_reg1_50_page_t *)(Dev_Lh5815_TxRx.Rx.Buf));
    s_drv_lt5815_read_reg51_69_page_t *read1 = ((s_drv_lt5815_read_reg51_69_page_t *)(Dev_Lh5815_TxRx.Rx.Buf));
    if (REF_FREE == Dev_Lh5815_TxRx.Rx.IsBusy) /* 空闲 */
    {
        if ((Dev_Lh5815_TxRx.Rx.Buf[0] == Dev_Lh5815_Addr) && (Dev_Lh5815_TxRx.Rx.Buf[1] == Dev_Lh5815_Read_Cmd))
        {

            len = read->byte_cnt + 3;
            crc = ((u16_t)(crc << 8)) + Dev_Lh5815_TxRx.Rx.Buf[len];
            crc = ((u16_t)(crc << 8)) + Dev_Lh5815_TxRx.Rx.Buf[len + 1];
            if (crc == CheckCrc_Tab_u16(Dev_Lh5815_TxRx.Rx.Buf, len))
            {
                if (read->byte_cnt == (sizeof(s_drv_lt5815_read_reg1_50_page_t) - 5))
                {
                    gDev_Lh5815.Ver = (gDev_Lh5815.Ver << 8) + read->ver_addr_1;
                    gDev_Lh5815.Ver = (gDev_Lh5815.Ver << 8) + read->ver_addr_0;

                    gDev_Lh5815.Weight = 0;
                    gDev_Lh5815.Weight = ((u32_t)(gDev_Lh5815.Weight << 8)) + read->js_zl_2;
                    gDev_Lh5815.Weight = ((u32_t)(gDev_Lh5815.Weight << 8)) + read->js_zl_1;
                    gDev_Lh5815.Weight = ((u32_t)(gDev_Lh5815.Weight << 8)) + read->js_zl_0;
                    if (read->js_zl_3 & SET_BIT(2))
                        gDev_Lh5815.Weight = gDev_Lh5815.Weight * (-1);

                    gDev_Lh5815.Cgq_Fx = ((u16_t)(gDev_Lh5815.Cgq_Fx << 8)) + read->cgq_fx_h8;
                    gDev_Lh5815.Cgq_Fx = ((u16_t)(gDev_Lh5815.Cgq_Fx << 8)) + read->cgq_fx_l8;

                    gDev_Lh5815.Fdz = ((u16_t)(gDev_Lh5815.Fdz << 8)) + read->fdz_h8;
                    gDev_Lh5815.Fdz = ((u16_t)(gDev_Lh5815.Fdz << 8)) + read->fdz_l8;
                    gDev_Lh5815.Kj_Zl_Fw = ((u16_t)(gDev_Lh5815.Kj_Zl_Fw << 8)) + read->kj_zl_fw_h8;
                    gDev_Lh5815.Kj_Zl_Fw = ((u16_t)(gDev_Lh5815.Kj_Zl_Fw << 8)) + read->kj_zl_fw_l8;
                    gDev_Lh5815.Mlc = ((u32_t)(gDev_Lh5815.Mlc << 8)) + read->mlc_3;
                    gDev_Lh5815.Mlc = ((u32_t)(gDev_Lh5815.Mlc << 8)) + read->mlc_2;
                    gDev_Lh5815.Mlc = ((u32_t)(gDev_Lh5815.Mlc << 8)) + read->mlc_1;
                    gDev_Lh5815.Mlc = ((u32_t)(gDev_Lh5815.Mlc << 8)) + read->mlc_0;
                }
                else if (read->byte_cnt == (sizeof(s_drv_lt5815_read_reg51_69_page_t) - 5))
                {
                    gDev_Lh5815.Cz_Dw = ((u16_t)(gDev_Lh5815.Cz_Dw << 8)) + read1->cz_dw_h8;
                    gDev_Lh5815.Cz_Dw = ((u16_t)(gDev_Lh5815.Cz_Dw << 8)) + read1->cz_dw_l8;
                }
            }
        }
        else if ((Dev_Lh5815_TxRx.Rx.Buf[0] == Dev_Lh5815_Addr) && (Dev_Lh5815_TxRx.Rx.Buf[1] == Dev_Lh5815_Write_Cmd))
        {
        }
        my_memclr_00(&Dev_Lh5815_TxRx.Rx, sizeof(Dev_Lh5815_TxRx.Rx));
    }
}
/**
 * @description:
 * @param {*}
 * @return {*}
 * @note:
 */
void Dev_Lh5815_Tx_Bd_0000(void) /* 标定 */
{
    Ram u16_t crc = 0;
    Ram u8_t cnt = 0;
    my_memclr_00(&Dev_Lh5815_TxRx.Tx, sizeof(Dev_Lh5815_TxRx.Tx));
    Dev_Lh5815_TxRx.Tx.Buf[cnt++] = Dev_Lh5815_Addr;                       /* 器件地址 */
    Dev_Lh5815_TxRx.Tx.Buf[cnt++] = Dev_Lh5815_Write_Cmd;                  /* 读命令 */
    Dev_Lh5815_TxRx.Tx.Buf[cnt++] = ((u8_t)(Dev_Lh5815_Bd_H16_Addr >> 8)); /* 即使重量地址高16位 */
    Dev_Lh5815_TxRx.Tx.Buf[cnt++] = ((u8_t)(Dev_Lh5815_Bd_H16_Addr >> 0)); /* 即使重量地址低16位 */
    Dev_Lh5815_TxRx.Tx.Buf[cnt++] = 0;                                     /* 写取寄存器数量 */
    Dev_Lh5815_TxRx.Tx.Buf[cnt++] = 2;                                     /* 写取寄存器数量 */
    Dev_Lh5815_TxRx.Tx.Buf[cnt++] = 4;                                     /* 参数个数 */
    Dev_Lh5815_TxRx.Tx.Buf[cnt++] = ((u8_t)(0 >> 24));
    Dev_Lh5815_TxRx.Tx.Buf[cnt++] = ((u8_t)(0 >> 16));
    Dev_Lh5815_TxRx.Tx.Buf[cnt++] = ((u8_t)(0 >> 8));
    Dev_Lh5815_TxRx.Tx.Buf[cnt++] = ((u8_t)(0 >> 0));
    crc = CheckCrc_Tab_u16(Dev_Lh5815_TxRx.Tx.Buf, cnt);

    Dev_Lh5815_TxRx.Tx.Buf[cnt++] = ((u8_t)(crc >> 8));
    Dev_Lh5815_TxRx.Tx.Buf[cnt++] = ((u8_t)(crc >> 0));

    Dev_Lh5815_Uart_Tx_Startup(13);
}
void Dev_Lh5815_Tx_Bd_5000(void) /* 标定 */
{
    Ram u16_t crc = 0;
    Ram u8_t cnt = 0;
    my_memclr_00(&Dev_Lh5815_TxRx.Tx, sizeof(Dev_Lh5815_TxRx.Tx));
    Dev_Lh5815_TxRx.Tx.Buf[cnt++] = Dev_Lh5815_Addr;                       /* 器件地址 */
    Dev_Lh5815_TxRx.Tx.Buf[cnt++] = Dev_Lh5815_Write_Cmd;                  /* 读命令 */
    Dev_Lh5815_TxRx.Tx.Buf[cnt++] = ((u8_t)(Dev_Lh5815_Bd_H16_Addr >> 8)); /* 即使重量地址高16位 */
    Dev_Lh5815_TxRx.Tx.Buf[cnt++] = ((u8_t)(Dev_Lh5815_Bd_H16_Addr >> 0)); /* 即使重量地址低16位 */
    Dev_Lh5815_TxRx.Tx.Buf[cnt++] = 0;                                     /* 写取寄存器数量 */
    Dev_Lh5815_TxRx.Tx.Buf[cnt++] = 2;                                     /* 写取寄存器数量 */
    Dev_Lh5815_TxRx.Tx.Buf[cnt++] = 4;                                     /* 参数个数 */
    Dev_Lh5815_TxRx.Tx.Buf[cnt++] = ((u8_t)(5000 >> 24));
    Dev_Lh5815_TxRx.Tx.Buf[cnt++] = ((u8_t)(5000 >> 16));
    Dev_Lh5815_TxRx.Tx.Buf[cnt++] = ((u8_t)(5000 >> 8));
    Dev_Lh5815_TxRx.Tx.Buf[cnt++] = ((u8_t)(5000 >> 0));
    crc = CheckCrc_Tab_u16(Dev_Lh5815_TxRx.Tx.Buf, cnt);
    Dev_Lh5815_TxRx.Tx.Buf[cnt++] = ((u8_t)(crc >> 8));
    Dev_Lh5815_TxRx.Tx.Buf[cnt++] = ((u8_t)(crc >> 0));

    Dev_Lh5815_Uart_Tx_Startup(13);
}

void Dev_Lh5815_Tx_Read_Reg1_50(void) /* 读数据 */
{
    Ram u16_t crc = 0;
    Ram u8_t cnt = 0;
    my_memclr_00(&Dev_Lh5815_TxRx.Tx, sizeof(Dev_Lh5815_TxRx.Tx));
    Dev_Lh5815_TxRx.Tx.Buf[cnt++] = Dev_Lh5815_Addr;                                       /* 器件地址 */
    Dev_Lh5815_TxRx.Tx.Buf[cnt++] = Dev_Lh5815_Read_Cmd;                                   /* 读命令 */
    Dev_Lh5815_TxRx.Tx.Buf[cnt++] = ((u8_t)(Dev_Lh5815_Ver_Addr >> 8));                    /* 即使重量地址高16位 */
    Dev_Lh5815_TxRx.Tx.Buf[cnt++] = ((u8_t)(Dev_Lh5815_Ver_Addr >> 0));                    /* 即使重量地址低16位 */
    Dev_Lh5815_TxRx.Tx.Buf[cnt++] = 0;                                                     /* 读取寄存器数量高16位 */
    Dev_Lh5815_TxRx.Tx.Buf[cnt++] = ((sizeof(s_drv_lt5815_read_reg1_50_page_t) - 5) >> 1); /* 读取寄存器数量低16位 */
    crc = CheckCrc_Tab_u16(Dev_Lh5815_TxRx.Tx.Buf, cnt);

    Dev_Lh5815_TxRx.Tx.Buf[cnt++] = ((u8_t)(crc >> 8));
    Dev_Lh5815_TxRx.Tx.Buf[cnt++] = ((u8_t)(crc >> 0));

    Dev_Lh5815_Uart_Tx_Startup(9);
}
void Dev_Lh5815_Tx_Read_Reg51_69(void) /* 读数据 */
{
    Ram u16_t crc = 0;
    Ram u8_t cnt = 0;
    my_memclr_00(&Dev_Lh5815_TxRx.Tx, sizeof(Dev_Lh5815_TxRx.Tx));
    Dev_Lh5815_TxRx.Tx.Buf[cnt++] = Dev_Lh5815_Addr;                                        /* 器件地址 */
    Dev_Lh5815_TxRx.Tx.Buf[cnt++] = Dev_Lh5815_Read_Cmd;                                    /* 读命令 */
    Dev_Lh5815_TxRx.Tx.Buf[cnt++] = ((u8_t)(Dev_Lh5815_Cz_Dw_Addr >> 8));                   /* 即使重量地址高16位 */
    Dev_Lh5815_TxRx.Tx.Buf[cnt++] = ((u8_t)(Dev_Lh5815_Cz_Dw_Addr >> 0));                   /* 即使重量地址低16位 */
    Dev_Lh5815_TxRx.Tx.Buf[cnt++] = 0;                                                      /* 读取寄存器数量高16位 */
    Dev_Lh5815_TxRx.Tx.Buf[cnt++] = ((sizeof(s_drv_lt5815_read_reg51_69_page_t) - 5) >> 1); /* 读取寄存器数量低16位 */
    crc = CheckCrc_Tab_u16(Dev_Lh5815_TxRx.Tx.Buf, cnt);

    Dev_Lh5815_TxRx.Tx.Buf[cnt++] = ((u8_t)(crc >> 8));
    Dev_Lh5815_TxRx.Tx.Buf[cnt++] = ((u8_t)(crc >> 0));

    Dev_Lh5815_Uart_Tx_Startup(8);
}
void Dev_Lh5815_Tx_Deal(void)
{
    static u8_t tx_tick = 0, i = 0;
    if (REF_FALSE == Drv_Autoload_Ntick_u8(&tx_tick, 200)) // 发送间隔超过100ms
        return;
    if (gDev_Lh5815.Tx_Cmd == dev_lh5815_tx_cmd_stan_bd_5000)
        Dev_Lh5815_Tx_Bd_5000();
    else if (gDev_Lh5815.Tx_Cmd == dev_lh5815_tx_cmd_stan_bd_0000)
        Dev_Lh5815_Tx_Bd_0000();
    else if (i & 0x01)
        Dev_Lh5815_Tx_Read_Reg1_50();
    else
        Dev_Lh5815_Tx_Read_Reg51_69();
    i++;
    gDev_Lh5815.Tx_Cmd = dev_lh5815_tx_cmd_standy;
}
void Dev_Lh5815_Scan(void)
{
    static u8_t lh5815_tick = 0;
    if (REF_FALSE == Drv_Autoload_Ntick_u8(&lh5815_tick, Dev_Lh5815_Task_Time))
        return;
    Dev_Lh5815_Tx_Status();
    Dev_Lh5815_Rx_Status();
    Dev_Lh5815_Rx_Deal();
    Dev_Lh5815_Tx_Deal();
}